package com.xebest.web.controller.logisticscar;

import com.alibaba.fastjson.JSONObject;
import com.xebest.common.base.controller.BASEMessage;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.web.form.XeVenCarForm;
import com.xebest.web.model.XeVenCarEntity;
import com.xebest.web.service.XeLogisticsCarService;
import com.xebest.web.view.ErpUserInfo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 物流车辆
 *
 * @author xzd
 */
@RestController
@RequestMapping("erp/logisticsCar")
public class XelogisticsCarController extends BaseController {
    private static Logger log = LoggerFactory.getLogger(XelogisticsCarController.class);
    @Autowired
    private XeLogisticsCarService xeLogisticsCarService;

    /**
     * 物流车辆管理列表
     *
     * @author xzd
     */
    @RequestMapping("queryLogisticsCarList")
    public BaseResponse queryLogisticsCarList(@RequestBody XeVenCarForm xeVenCar) {
        String paramJson = JSONObject.toJSONString(xeVenCar);
        log.info("物流车辆管理列表,传入参数： {}", paramJson);

        // 2、拼装参数,
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        XeVenCarEntity xeVenCarEntity = JSONObject.parseObject(paramJson, XeVenCarEntity.class);
        xeVenCarEntity.setUserCode(userInfo.getUserCode());
        // 3、返回结果
        try {
            Map<String, Object> retMap = xeLogisticsCarService.queryErpLogisticsCarList(xeVenCarEntity, xeVenCar.getPageNum(), xeVenCar.getPageSize());
            return sendAjaxOK(retMap);
        } catch (Exception e) {
            log.info("物流车辆管理列表异常");
            return sendAjaxError(" 查询商家商品列表异常");
        }
    }


    /**
     * 修改物流车辆
     *
     * @author xzd
     */
    @RequestMapping("updateLogisticsCar")
    public BaseResponse updateLogisticsCar(@RequestBody XeVenCarForm xeVenCar) {
        log.info("修改车辆路线传入参数：" + JSONObject.toJSONString(xeVenCar));
        if (StringUtils.isBlank(xeVenCar.getCarPlateNum()) || StringUtils.isBlank(xeVenCar.getMobilePhone())
                || xeVenCar.getCarId() == null) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        XeVenCarEntity carEntity = JSONObject.parseObject(JSONObject.toJSONString(xeVenCar), XeVenCarEntity.class);
        carEntity.setCarPlateNum(xeVenCar.getCarPlateNum().toUpperCase());
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        carEntity.setUserCode(userInfo.getUserCode());
        try {
            int num = xeLogisticsCarService.updateErpLogisticsCar(carEntity);
            if (num > 0) {
                initLogisticsCar(carEntity);
                return sendAjaxOK();
            } else if (num == -1) {
                return sendAjaxError("不能修改其他供应商的车辆");
            }
        } catch (Exception e) {
            log.info("修改物流车辆异常", e);
            return sendAjaxError("修改物流车辆异常");
        }
        return sendAjaxError();
    }


    /**
     * 校验车牌号是否重复
     *
     * @author xzd
     */
    @RequestMapping("checkCarNum")
    public BaseResponse checkCarNum(@RequestBody XeVenCarForm xeVenCarForm) {
        log.info(" 校验车牌号是否重复：" + JSONObject.toJSONString(xeVenCarForm));
        if (StringUtils.isBlank(xeVenCarForm.getCarPlateNum())) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        try {
            int num = xeLogisticsCarService.checkCarNum(xeVenCarForm.getCarPlateNum());
            if (num == 0) {
                return sendRemoteAjaxOK();
            } else {
                return sendRemoteAjaxError("系统中已存在此车牌号");
            }

        } catch (Exception e) {
            log.info("校验车牌号异常", e);
            return sendRemoteAjaxError("校验车牌号异常");
        }
    }

    /**
     * 初始化物流车辆
     *
     * @author xzd
     */
    public void initLogisticsCar(XeVenCarEntity xeVenCarEntity) {
        log.info("=============== 初始化物流车辆 =============== start");
        try {
            xeLogisticsCarService.initLogisticsCar(xeVenCarEntity);
        } catch (Exception e) {
            log.error("初始化物流车辆异常", e);
        }
    }

}
  
